/**
 * @file 登录日志
 * @date 2025/11/18 09:27:14
 * @author lyqjob@yeah.net
 */

import {  useState ,useEffect } from 'react';
import { Table, Tag, Space } from 'antd';

import { adminApi } from "@/services/admin";
import dayjs from '@/utils/dayjs.js';



const Loginlog = () => {
    const [data,setData] = useState([]);
    const [loading,setLoading] =useState(true)
    const [pageSize,setPageSize] = useState(10)
    const [current,setCurrent] = useState(1)
    const [total,setTotal] = useState(0)


    const columns = [
        { 
            title: '序号', 
            dataIndex: 'operation', 
            key: '' ,
            width:100,
            fixed: 'left',
            render:(text,record,index) => (
                `${index+1}`
            )
        },
        { title: '用户名称', dataIndex: 'opername', key: '' ,width:150},
        { title: '登录地址', dataIndex: 'client_ip', key: '' ,width:150},
        { title: '登录地点', dataIndex: 'location', key: '' ,width:150},
        { title: '浏览器信息', dataIndex: 'operating_system', key: '' ,width:200},
        { title: '操作系统', dataIndex: 'operating_system', key: '' ,width:150},

        {
            title: '登录状态', dataIndex: 'operation_desc', key: '',width:100,
            render: (tag) => {
                return (
                    <Tag color={tag === '1' ? 'green' : 'red'} key={tag}>
                        {tag === '1' ? '成功' : '失败'}
                    </Tag>
                )
            }
        },
        
        { 
            title: '登录时间', dataIndex: 'operation_time', key: '' , width:200,
            fixed: 'right',
            render:(text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
        }
        
    ];



    useEffect(()=>{
        getList(current,pageSize)
    },[])


    // 获取操作日志列表
    const getList = async (page,limit)=>{
        
        const [err,res] = await adminApi.getOperlogs({page,limit,business_type:'login'});
        if(err){
            setLoading(false)
            // message.warning('获取日志列表失败')
            return
        }
        if(res?.code === 200 && res?.data?.data){
            setLoading(false)
            setData(res.data.data)
            setTotal(res.data.pagination.total)
            window.process.done();
        }

    }

    return (
        <div className="container">
            <Space>
               
            </Space>
            <Table
                loading={loading}
                rowKey={'id'}
                columns={columns}
                rowSelection={false}
                dataSource={data}
                scroll={{
                    x: 1000,
                    y: 450
                }}
                pagination={{
                    pageSize,
                    current,
                    showPage:true,
                    total:total,
                    showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`,
                    onChange:(page,pageSize)=>{
                        setPageSize(pageSize)
                        setCurrent(page)
                        getList(page,pageSize)
                    }    
                }}
            />

             
        </div>
    )
}


export default Loginlog;
